package quartz.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.util.StopWatch;

/**
 * Step 耗时监听
 */
@Slf4j
public class StepConsumingTimeListener implements StepExecutionListener {
    private final StopWatch stopWatch = new StopWatch();

    @Override
    public void beforeStep(StepExecution stepExecution) {
        stopWatch.start();
        log.info("Step before " + stepExecution.getJobParameters());
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        stopWatch.stop();
        log.info("Step Status: {}", stepExecution.getStatus());
        if (stepExecution.getStatus() == BatchStatus.COMPLETED) {
            log.info("Step Finished");
        } else if (stepExecution.getStatus() == BatchStatus.FAILED) {
            log.info("Step Failed");
        }
        log.info("Step Cost Time: {} s", stopWatch.getTotalTimeSeconds());
        return stepExecution.getExitStatus();
    }
}